﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Quartz;
using XsqTech.SmartOA.Dtos.XSPDto.FormDto;
using XsqTech.SmartOA.IService;
using XsqTech.SmartOA.Service.DTO;

namespace XsqTech.SmartOA.Application.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TimingController : ControllerBase
    {
        ITimingService _timingService;

        public TimingController(ITimingService timingService)
        {
            _timingService = timingService;
        }

        /// <summary>
        ///  获取全部Job任务信息
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTimingList(string? name, int index, int size)
        {
            if (index<=0)
            {
                index = 1;
            }
            if (size<=1)
            {
                size = 6;
            }
            return Ok(_timingService.GetTimingList(name,index,size));
        }

        /// <summary>
        /// 删除Job任务信息(单删)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DeleteTiming(string id)
        {
            return Ok(_timingService.DeleteTiming(id));
        }

        /// <summary>
        /// 删除Job任务信息（批删）
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult AllFormDelete(List<TimingDto> dto)
        {
            return Ok(_timingService.AllFormDelete(dto));
        }

        /// <summary>
        /// 添加Job任务信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddTiming(TimingDto dto)
        {
            if (!ModelState.IsValid)
            {
                return Ok(new ResponseModel() { Code = State.数据未导入, Message = "数据未导入" });
            }
            return Ok(_timingService.AddTiming(dto));
        }
        /// <summary>
        /// 反填Job任务信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult TimingQueryId(string id)
        {
            return Ok(_timingService.TimingQueryId(id));
        }
        /// <summary>
        /// 修改Job任务信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateTiming(TimingDto dto)
        {
            if (!ModelState.IsValid)
            {
                return Ok(new ResponseModel() { Code = State.数据未导入, Message = "数据未导入" });
            }
            return Ok(_timingService.UpdateTiming(dto));
        }
        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateState(TimingDto dto)
        {
            if (!ModelState.IsValid)
            {
                return Ok(new ResponseModel() { Code = State.数据未导入, Message = "数据未导入" });
            }
            return Ok(_timingService.UpdateState(dto));
        }


        /// <summary>
        /// 启动Job任务
        /// </summary>
        /// <param name="data">Job信息</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult StartScheduleJob(TimingDto data)
        {
            if (!ModelState.IsValid)
            {
                return Ok(new ResponseModel() { Code = State.数据未导入, Message = "数据未导入" });
            }
            return Ok(_timingService.StartScheduleJob(data));
        }
        /// <summary>
        /// 反射拿取全部继承了IJob接口的命名路由
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult After()
        {
            var result = _timingService.After();
            return Ok(result);
        }
        /// <summary>
        ///暂停Job任务
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult PauseScheduleJob(TimingDto data)
        {
            var result = _timingService.PauseScheduleJob(data);
            return Ok(result);
        }
    }
}
